package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.app.MyApplication;
import com.dingwei.superiordiary.bean.CommodityDetailsBean;
import com.dingwei.superiordiary.mvp.presenter.IntegerDetailsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.utils.MathUtil;
import com.lihao.baselibrary.weight.TitleBar;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/2/27.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class IntegerDetailsActivity extends BaseActivity implements Contract.IntegerDetailsView {
    IntegerDetailsPresenter integerDetailsPresenter = new IntegerDetailsPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.img)
    ImageView img;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_point)
    TextView tvPoint;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.webview)
    WebView webview;
    @Bind(R.id.btn_exchange)
    Button btnExchange;
    @Bind(R.id.data_layout)
    LinearLayout dataLayout;
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;

    private String id;
    private CommodityDetailsBean bean;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_integral_details;
    }

    @Override
    protected void initData() {
        integerDetailsPresenter.initData(id);
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void getData(CommodityDetailsBean bean) {
        this.bean = bean;
        showData("success");
        Glide.with(BaseActivity.context)
                .load(bean.data.icon)
                .apply(MyApplication.options_no_transform)
                .into(img);
        tvName.setText(bean.data.name);
        tvPoint.setText(bean.data.integral + "次元币");
        tvPrice.setText("市场价￥" + MathUtil.trimShu(bean.data.price + ""));
        webview.loadUrl(URLS.HOST + "product/getDescription?id=" + getIntent().getStringExtra("id"));
    }

    @Override
    public void onFail() {
        showData("fail");
    }

    public void showData(String state) {
        if (state.equals("success")) {
            dataLayout.setVisibility(View.VISIBLE);
            noNetLayout.setVisibility(View.GONE);
        } else {
            dataLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.btn_exchange, R.id.no_net_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_exchange:
                intent = new Intent(BaseActivity.context,FirmOrderActivity.class);
                intent.putExtra("bean",bean.data);
                intent.putExtra("id",bean.data.id);
                intent.putExtra("num",1);
                startActivity(intent);
                break;
            case R.id.no_net_btn:
                integerDetailsPresenter.initData(id);
                break;
        }
    }
}
